#ifndef __DRV_RF24L01_H__
#define __DRV_RF24L01_H__

#include "drv_spi.h"
#include "stdint.h"

/** 配置和选项定义 */
#define PIPE0_PACKET_LEN            6
#define PIPE1_PACKET_LEN            0
#define PIPE2_PACKET_LEN            0
#define PIPE3_PACKET_LEN            0
#define PIPE4_PACKET_LEN            0
#define PIPE5_PACKET_LEN            0
#define REPEAT_CNT                  ARC_3TIMES
#define AUTO_RETRANSMISSION_DELAY   ARD_500US
#define INIT_ADDR                   0x34,0x43,0x10,0x10,0x01 //地址

/*类型定义*/
typedef enum 
{
    MODE_RX = 0,
    MODE_TX = 1
} nRf24l01ModeType;


/** RF24L01硬件接口定义 */
#define RF24L01_CE_GPIO_PORT            GPIOA
#define RF24L01_CE_GPIO_CLK             RCC_APB2Periph_GPIOA
#define RF24L01_CE_GPIO_PIN             GPIO_Pin_0

#define RF24L01_IRQ_GPIO_PORT           GPIOA
#define RF24L01_IRQ_GPIO_CLK            RCC_APB2Periph_GPIOA
#define RF24L01_IRQ_GPIO_PIN            GPIO_Pin_1

#define RF24L01_CS_GPIO_PORT            SPI_NSS_GPIO_PORT
#define RF24L01_CS_GPIO_CLK             SPI_NSS_GPIO_CLK
#define RF24L01_CS_GPIO_PIN             SPI_NSS_GPIO_PIN


/** 口线操作函数定义 */
#define RF24L01_SET_CE_HIGH( )              RF24L01_CE_GPIO_PORT->ODR |= RF24L01_CE_GPIO_PIN
#define RF24L01_SET_CE_LOW( )               RF24L01_CE_GPIO_PORT->ODR &= (uint32_t)( ~(uint32_t)RF24L01_CE_GPIO_PIN )

#define RF24L01_SET_CS_HIGH( )              spi_set_nss_high( )
#define RF24L01_SET_CS_LOW( )               spi_set_nss_low( )
 
//命令
#define NRF_READ_REG             0x00    //读配置寄存器，低5位为寄存器地址
#define NRF_WRITE_REG            0x20    //写配置寄存器，低5位为寄存器地址
#define RD_RX_PLOAD              0x61    //读RX有效数据，1~32字节
#define WR_TX_PLOAD              0xA0    //写TX有效数据，1~32字节
#define FLUSH_TX                 0xE1    //清除TX FIFO寄存器，发射模式下使用
#define FLUSH_RX                 0xE2    //清除RX FIFO寄存器，接收模式下使用
#define REUSE_TX_PL              0xE3    //重新使用上一包数据，CE为高，数据包被不断发送
#define R_RX_PL_WID              0x60
#define NOP                      0xFF    //空操作，可以用来读状态寄存器
#define W_ACK_PLOAD              0xA8
#define WR_TX_PLOAD_NACK         0xB0

//寄存器地址
#define CONFIG                   0x00    //配置寄存器地址
#define EN_AA                    0x01    //使能自动应答功能 bit0~5 对应通道0~5
#define EN_RXADDR                0x02    //接收地址允许 bit0~5 对应通道0~5
#define SETUP_AW                 0x03    //设置地址宽度(所有数据通道) bit0~1: 00,3字节 01,4字节, 02,5字节
#define SETUP_RETR               0x04    //建立自动重发;bit0~3:自动重发计数器;bit4~7:自动重发延时 250*x+86us
#define RF_CH                    0x05    //RF通道,bit0~6工作通道频率
#define RF_SETUP                 0x06    //RF寄存器，bit3:传输速率( 0:1M 1:2M);bit1~2:发射功率;bit0:噪声放大器增益
#define STATUS                   0x07    //状态寄存器
#define OBSERVE_TX               0x08    //发送检测寄存器,bit7~4:数据包丢失计数器;bit3~0:重发计数器
#define RPD_REG                  0x09    //载波检测寄存器,bit0:载波检测
#define RX_ADDR_P0               0x0A    //数据通道0接收地址，最大长度5个字节，低字节在前
#define RX_ADDR_P1               0x0B    //数据通道1接收地址，最大长度5个字节，低字节在前
#define RX_ADDR_P2               0x0C    //数据通道2接收地址，最低字节可设置，高字节，必须同RX_ADDR_P1[39:8]相等
#define RX_ADDR_P3               0x0D    //数据通道3接收地址，最低字节可设置，高字节，必须同RX_ADDR_P1[39:8]相等
#define RX_ADDR_P4               0x0E    //数据通道4接收地址，最低字节可设置，高字节，必须同RX_ADDR_P1[39:8]相等
#define RX_ADDR_P5               0x0F    //数据通道5接收地址，最低字节可设置，高字节，必须同RX_ADDR_P1[39:8]相等
#define TX_ADDR                  0x10    //发送地址(低字节在前),ShockBurstTM模式下，RX_ADDR_P0与地址相等
#define RX_PW_P0                 0x11    //接收数据通道0有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P1                 0x12    //接收数据通道1有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P2                 0x13    //接收数据通道2有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P3                 0x14    //接收数据通道3有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P4                 0x15    //接收数据通道4有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P5                 0x16    //接收数据通道5有效数据宽度(1~32字节),设置为0则非法
#define NRF_FIFO_STATUS          0x17    //FIFO状态寄存器
#define DYNPD                    0x1C
#define FEATRUE                  0x1D

//位定义
#define MASK_RX_DR              ( 0x01 << 6 )
#define MASK_TX_DS              ( 0x01 << 5 )
#define MASK_MAX_RT             ( 0x01 << 4 )
#define EN_CRC                  ( 0x01 << 3 )
#define CRC_1BYTE               ( 0x00 << 2 )
#define CRC_2BYTE               ( 0x01 << 2 )
#define PWR_UP                  ( 0x01 << 1 )
#define PRIM_RX                 ( 0x01 << 0 )
                      
#define ENAA_NO_PIPE            ( 0x00 << 0 )
#define ENAA_P5                 ( 0x01 << 5 )
#define ENAA_P4                 ( 0x01 << 4 )
#define ENAA_P3                 ( 0x01 << 3 )
#define ENAA_P2                 ( 0x01 << 2 )
#define ENAA_P1                 ( 0x01 << 1 )
#define ENAA_P0                 ( 0x01 << 0 )
                      
#define ERX_NO_PIPE             ( 0x00 << 5 )
#define ERX_P5                  ( 0x01 << 5 )
#define ERX_P4                  ( 0x01 << 4 )
#define ERX_P3                  ( 0x01 << 3 )
#define ERX_P2                  ( 0x01 << 2 )
#define ERX_P1                  ( 0x01 << 1 )
#define ERX_P0                  ( 0x01 << 0 )
                      
#define AW_3BYTES               0x01
#define AW_4BYTES               0x02
#define AW_5BYTES               0x03

#define ARD_250US               ( 0x00 << 4 )
#define ARD_500US               ( 0x01 << 4 )
#define ARD_750US               ( 0x02 << 4 )
#define ARD_1000US              ( 0x03 << 4 )
#define ARD_1250US              ( 0x04 << 4 )
#define ARD_1500US              ( 0x05 << 4 )
#define ARD_1750US              ( 0x06 << 4 )
#define ARD_2000US              ( 0x07 << 4 )
#define ARD_2250US              ( 0x08 << 4 )
#define ARD_2500US              ( 0x09 << 4 )
#define ARD_2750US              ( 0x0A << 4 )
#define ARD_3000US              ( 0x0B << 4 )
#define ARD_3250US              ( 0x0C << 4 )
#define ARD_3500US              ( 0x0D << 4 )
#define ARD_3750US              ( 0x0E << 4 )
#define ARD_4000US              ( 0x0F << 4 )
#define ARC_DISABLE             0x00
#define ARC_1TIMES              0x01
#define ARC_2TIMES              0x02
#define ARC_3TIMES              0x03
#define ARC_4TIMES              0x04
#define ARC_5TIMES              0x05
#define ARC_6TIMES              0x06
#define ARC_7TIMES              0x07
#define ARC_8TIMES              0x08
#define ARC_9TIMES              0x09
#define ARC_10TIMES             0x0A
#define ARC_11TIMES             0x0B
#define ARC_12TIMES             0x0C
#define ARC_13TIMES             0x0D
#define ARC_14TIMES             0x0E
#define ARC_15TIMES             0x0F

#define CONT_WAVE               ( 0x01 << 7 )
#define RF_DR_LOW_250K          ( 0x04 << 3 )
#define RF_DR_HIGH_1M           ( 0x00 << 3 )
#define RF_DR_HIGH_2M           ( 0x01 << 3 )
#define PWR_F18DB               ( 0x00 << 1 )
#define PWR_F12DB               ( 0x01 << 1 )
#define PWR_F6DB                ( 0x02 << 1 )
#define PWR_0DB                 ( 0x03 << 1 )

#define RX_DR                   ( 0x01 << 6 )
#define TX_DS                   ( 0x01 << 5 )
#define MAX_RT                  ( 0x01 << 4 ) 
#define TX_FULL_STATUS          ( 0x01 << 0 )

#define RPD                     0 

#define TX_REUSE                ( 0x01 << 6 ) 
#define TX_FULL_FIFO_STATUS     ( 0x01 << 5 )
#define TX_EMPTY                ( 0x01 << 4 )
#define RX_FULL                 ( 0x01 << 1 )
#define RX_EMPTY                ( 0x01 << 0 )

#define DPL_NO_PIPE             ( 0x00 << 0 )
#define DPL_P5                  ( 0x01 << 5 )
#define DPL_P4                  ( 0x01 << 4 )
#define DPL_P3                  ( 0x01 << 3 )
#define DPL_P2                  ( 0x01 << 2 )
#define DPL_P1                  ( 0x01 << 1 )
#define DPL_P0                  ( 0x01 << 0 )

#define FEATURE_ALL_DISABLE     ( 0x00 << 0 )
#define EN_DPL                  ( 0x01 << 2 )
#define EN_ACK_PAY              ( 0x01 << 1 )
#define EN_DYN_ACK              ( 0x01 << 0 )
                       
#define IRQ_ALL                 ( RX_DR | TX_DS | MAX_RT )



uint8_t NRF24L01_Read_Reg( uint8_t RegAddr );
void NRF24L01_Read_Buf( uint8_t RegAddr, uint8_t *pBuf, uint8_t len );
void NRF24L01_Write_Reg( uint8_t RegAddr, uint8_t Value );
void NRF24L01_Write_Buf( uint8_t RegAddr, uint8_t *pBuf, uint8_t len );
void NRF24L01_Flush_Tx_Fifo ( void );
void NRF24L01_Flush_Rx_Fifo( void );
void NRF24L01_Reuse_Tx_Payload( void );
uint8_t NRF24L01_Nop( void );
uint8_t NRF24L01_Read_Status_Register( void );
uint8_t NRF24L01_Clear_IRQ_Flag( uint8_t IRQ_Source );
uint8_t NRF24L01_Read_IRQ_Status( uint8_t IRQ_Source );
uint8_t NRF24L01_Read_Top_Fifo_Width( void );
uint8_t NRF24L01_Read_Rx_Payload( uint8_t *pRxBuf );
void NRF24L01_Write_Tx_Payload_Ack( uint8_t *pTxBuf, uint8_t len );
void NRF24L01_Write_Tx_Payload_NoAck( uint8_t *pTxBuf, uint8_t len );
void NRF24L01_Write_Tx_Payload_InAck( uint8_t *pData, uint8_t len );
void NRF24L01_Set_TxAddr( uint8_t *pAddr, uint8_t len );
void NRF24L01_Set_RxAddr( uint8_t PipeNum, uint8_t *pAddr, uint8_t Len );
void NRF24L01_Set_Speed( uint8_t Speed );
void NRF24L01_Set_Power( uint8_t Power );
void NRF24LL01_Set_FreqPoint( uint8_t FreqPoint );
void NRF24L01_Set_Mode( nRf24l01ModeType Mode );
void NRF24L01_Gpio_Init( void );
void NRF24L01_Init( void );
void NRF24L01_Trig_Tx( void );
uint8_t NRF24L01_Read_ReTransmission_Count( void );
uint8_t NRF24L01_Read_LostPackets_Count( void );
void NRF24L01_PowerUp( void );
void NRF24L01_Standby( void );
#endif

